﻿using System;
using System.Windows.Forms;
using System.ComponentModel;
using Relocation.ControlLibrary;


namespace System.Windows.Forms
{


	/// <summary>
	/// 日期选择控件，可以为空
	/// </summary>
	public partial class DateTimePic : Control, IControlExpend
	{
		/// <summary>
		/// 是否为空
		/// </summary>
		private bool isNull = true;

		public event EventHandler ValueChanged;

		private DateTime _MaxDate = Convert.ToDateTime("9998-12-31");
		[DefaultValue("9998-12-31"), Description("MaxDate"), Category("最大日期")]//属性窗口中的相关说明
		public DateTime MaxDate
		{
			get
			{
				return _MaxDate;
			}
			set
			{
				_MaxDate = value;
				this.dateTimePicker1.MaxDate = value;
			}
		}

		private DateTime _MinDate = Convert.ToDateTime("1753-1-1");
		[DefaultValue("1753-1-1"), Description("MinDate"), Category("最小日期")]//属性窗口中的相关说明
		public DateTime MinDate
		{
			get
			{
				return _MinDate;
			}
			set
			{
				_MinDate = value;
				this.dateTimePicker1.MinDate = value;
			}
		}

		private DateTimePickerFormat _Format = DateTimePickerFormat.Long;
		[DefaultValue(DateTimePickerFormat.Long), Description("Format"), Category("确定日期时间是使用标准格式还是用自定义格式显示")]//属性窗口中的相关说明
		public DateTimePickerFormat Format
		{
			get
			{
				return _Format;
			}
			set
			{
				_Format = value;
				this.dateTimePicker1.Format = value;

			}
		}

		private string _CustomFormat = "";
		[DefaultValue(""), Description("CustomFormat"), Category(" 表示自定义日期/时间格式的字符串。")]//属性窗口中的相关说明
		public string CustomFormat
		{
			get
			{
				return _CustomFormat;
			}
			set
			{
				_CustomFormat = value;
				this.dateTimePicker1.CustomFormat = _CustomFormat;
			}
		}
		

		public DateTimePic()
		{
			InitializeComponent();
		}

		/// <summary>
		/// 用户选择的日期，可以为null
		/// </summary>
		[System.ComponentModel.DesignerSerializationVisibility(System.ComponentModel.DesignerSerializationVisibility.Hidden)]
		public System.Nullable<DateTime> Value
		{
			get
			{
				if (this.textBox1.Text.Equals(string.Empty))
				{
					return null;
				} else
				{
					return Convert.ToDateTime(this.textBox1.Text);
				}
			}
			set
			{
				if (value == null)
					this.textBox1.Text = string.Empty;
				else
				{
					this.dateTimePicker1.Value = (DateTime)value;
					this.dateTimePicker1_ValueChanged(null, null);	//如果时间没有改变时，需要手动触发，以让时间显示出来					
				}
				if (ValueChanged != null)
					ValueChanged(null, null);
			}
		}

		/// <summary>
		/// 将日期控件的值设为空
		/// </summary>
		public void setNull()
		{
			this.Value = null;
		}

		private void textBox1_Click(object sender, EventArgs e)
		{
			this.textBox1.SelectAll();
		}


		/// <summary>
		/// 当用户按下指定键后删除时间内容
		/// </summary>		
		private void textBox1_KeyUp(object sender, KeyEventArgs e)
		{
			//if (e.KeyCode == Keys.Back || e.KeyCode == Keys.Delete)
			//{
			//    this.setNull();
			//}
		}

		/// <summary>
		/// 显示时间选择框前赋值
		/// </summary>		
		private void dateTimePicker1_DropDown(object sender, EventArgs e)
		{

			isNull = false;
			if (this.textBox1.Text.Equals(string.Empty))
				this.dateTimePicker1.Value = System.DateTime.Now;
			else
			{
				try
				{
					this.dateTimePicker1.Value = Convert.ToDateTime(this.textBox1.Text);
				} catch
				{
					this.dateTimePicker1.Value = System.DateTime.Now;
				}
			}
			isNull = true;
		}

		private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
		{
			if (isNull)
			{	
				switch (this.Format)
				{
					case DateTimePickerFormat.Long:
						this.textBox1.Text = this.dateTimePicker1.Value.ToLongDateString();
						break;
					case DateTimePickerFormat.Short:
						this.textBox1.Text = this.dateTimePicker1.Value.ToShortDateString();
						break;
					case DateTimePickerFormat.Time:
						this.textBox1.Text = this.dateTimePicker1.Value.ToString();
						break;
					case DateTimePickerFormat.Custom:
						this.textBox1.Text = this.dateTimePicker1.Value.ToString(_CustomFormat);
						break;
					default:
						this.textBox1.Text = this.dateTimePicker1.Value.ToString();
						break;
				}
			}
		}



		private void DateTimePic_EnabledChanged(object sender, EventArgs e)
		{
			if (this.Enabled == false)
			{
				this.textBox1.BackColor = System.Drawing.SystemColors.Control;
			} else
			{
				this.textBox1.BackColor = System.Drawing.SystemColors.Window;
			}

		}

		//private override string _Text;

		public override string Text
		{
			set
			{
				DateTime dateTime;
				if (!String.IsNullOrEmpty(value) && DateTime.TryParse(value, out dateTime))
				{
					this.Value = dateTime;
				} else
				{
					this.Value = null;
				}
			}
			get
			{
				if (this.Value == null)
					return string.Empty;
				DateTime dateTime = ((DateTime)this.Value);
				switch (this.Format)
				{
					case DateTimePickerFormat.Long:
						return dateTime.ToLongDateString();
					case DateTimePickerFormat.Short:
						return dateTime.ToShortDateString();

					case DateTimePickerFormat.Time:
						return dateTime.ToString();
					case DateTimePickerFormat.Custom:
						return dateTime.ToString(_CustomFormat);
					default:
						return dateTime.ToString();
				}
			}
		}

		private void textBox1_Validating(object sender, CancelEventArgs e)
		{
			DateTime dateTime;
			if (DateTime.TryParse(this.textBox1.Text, out dateTime))
			{
				this.Value = dateTime;
			} else
			{
				this.textBox1.Text = string.Empty;
			}

		}

		//public override void ResetText()
		//{
		//    base.ResetText();
		//    this.setNull();
		//}


		#region IControlExpend 成员

		public object GetValue()
		{
			return this.Value;
		}

		public void SetValue(object obj)
		{
			this.Value = obj as DateTime?;
		}

		#endregion
	
	}



}
